<?php
/**
 * Created by PhpStorm.
 * User: Auser
 * Date: 2017/1/17
 * Time: 15:51
 */
namespace App\Helper;
class tool
{
    /**根据题号获取相应的权重或者值
     * @param $answers
     * @param $index
     * @param $valueType
     * @return null
     */
 public  static function getValue($answers,$index,$valueType){
        $value = null;
        foreach($answers as $k => $v){
            if($v['index'] == $index){
                $value = $answers[$k][$valueType];
            }
        }
        return $value;
    }

    /**将答案数组组装成字符串
     * @param $answers
     * @return string
     */
 public static  function arrToStr($answers){
        $arr = [];
        foreach($answers as $k => $v){
            $arr[0]=$v['index'];
            $arr[1]=$v['weight'];
            $arr[2]=$v['answer'];
            $str=implode(":",$arr);
            $answers[$k]=$str;
        }
        $answerStr = implode("##",$answers);
        return $answerStr;
    }


    /***将答案字符串转换为数组
     * @param $answerStr
     * @return array
     */
  public static function strToArr($answerStr){
        $answers = [];
        $answerArr = explode('##',$answerStr);
        if(count($answerArr)>0){
            foreach ($answerArr as $answer){
                $singleArr = explode(':',$answer);
                if(count($singleArr)>0){
                    $obj['index'] = $singleArr[0];
                    $obj['weight'] = $singleArr[1];
                    $obj['answer'] = $singleArr[2];
                    array_push($answers,$obj);
                }
            }
        }
        return $answers;
    }
}
